/*******************************************************************************
*   			Impacts of a Large-Scale Parenting Program: 				   *
*					Experimental Evidence from Chile						   *
********************************************************************************


	REQUIRES:	"${dt_final}panel_RR_indexes.dta"
								
	CREATES:	${out_tables}/T10_mediation_socioemotional.xls
				
	WRITEN BY:  Italo Lopez  [italolop@usc.edu]

********************************************************************************
	Prepare data
*******************************************************************************/
	
	
	* Load Data 
	use "${dt_final}/panel_RR_indexes.dta", clear

	gen femalechild=(gender==2)
	tab age_year, g(age)


/*******************************************************************************
*   Set macros for regressions
*******************************************************************************/
			
	* Controls
	*-----------------------------------------------------------
	macro  def Xvar1 "femalechild age2-age6"  	
	macro  def Xvar2 "i.age_year i.gender inc_qtaut_old i.pc_edu_mdsfin hh_mem hh_tipo"  	
    macro  def Xvar2old "i.gender inc_qtaut_old i.pc_edu_mdsfin_old hh_mem hh_tipo"  

	* Parental characteristics: WAIS and BFI and imputed wais 
	*-----------------------------------------------------------
	macro def parent_cha4 "BFI_ext BFI_ope BFI_con BFI_neu BFI_agr wais2 flag_wais"


	xtset CENTRO_SALUD

	g TIPOB=TIPO if TIPO!=3
	g TIPOI=TIPO if TIPO!=2		



	rename zptevir_irtscore2 zpt_irts2
	rename zibatt_score z_batt
	
	
	

/*******************************************************************************
	Table 10: Mediation analysis: Socioemotional Index
*******************************************************************************/

	local finaloutcomes "zsoc_index " 
	local intermediate "zfbeh_gsem zfbel_gsem"	


	foreach var of local finaloutcomes {
		foreach med_var of local intermediate {

					xi: xtreg `var' i.TIPO `med_var' $Xvar1, fe i(CENTRO_SALUD)
					estimates store `var'_`med_var'					
				}
			xi: xtreg `var' i.TIPO $Xvar1, fe i(CENTRO_SALUD)
			estimates store `var'_med0				
			xi: xtreg `var' i.TIPO zfbeh_gsem zfbel_gsem $Xvar1, fe i(CENTRO_SALUD)
			estimates store `var'_med1

	}

	foreach var of local finaloutcomes {
		
			esttab `var'_med0 `var'_zfbeh_gsem `var'_zfbel_gsem `var'_med1	  /*	
			*/ using "${out_tables}/T10_mediation_socioemotional.csv", replace f 		  /*
			*/ cells("b(fmt(3)star)" "se(fmt(3)par)") 						  /*
			*/ star(* 0.10 ** 0.05 *** 0.01) 								  /*
			*/ stats(r2 N, fmt(2 0 3) labels("R-squared" 		  /*
			*/ "Observations")) 		  /*
			*/ legend	keep(_ITIPO_2 _ITIPO_3 zfbeh_gsem zfbel_gsem )		  /*
			*/ mtitles(	"Base"	"+Behavioral Index"	"+Beliefs Index"		  /*
			*/ "+Significant Mediators") 									  /*
			*/ varlabels(_ITIPO_2 "NEP-B" _ITIPO_3 "NEP-I" 					  /*
			*/ zfbeh_gsem "Caregiver Behavioral Index" 						  /*
			*/ zfbel_gsem "Caregiver Beliefs Index")
			
	}

